﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Benefit.Models.Sys;
namespace Benefit.Service.Day
{
    /// <summary>
    /// 账户资金变动(对于个人多账号后显示的总计值在while循环后重新写一个对象写入)这个是显示在第一行的总数(u)
    /// </summary>
    public class ServiceAccountDayChange : Benefit.Interface.Day.IAccountDayChange
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountDayChange(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.AccountDayChange where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.AccountDayChange c in query)
            {
                db.AccountDayChange.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存账号日变化情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitDayAccountDayChange(int tradeHistoryId, string pdate)
        {
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            Interface.Sys.IAccount _account = new Sys.ServiceAccount(db);
            Interface.ServerData.IT_TradeDetail_History tth = new ServerData.ServiceT_TradeDetail_History(db);
            Interface.ServerData.IT_SettlementMoney money = new ServerData.ServiceT_SettlementMoney(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            foreach (Models.ServerData.T_Account account in serverAccounts)
            {
                Models.Day.AccountDayChange change = new Models.Day.AccountDayChange();
          
                change.AccountId = _account.GetAccountId(account.LoginAccount);
           
                change.BillCount = tth.GetDayBillCount(pdate, account.Id);
                if (change.BillCount > 0)
                {
          
                    Models.ServerData.T_SettlementMoney _money = money.GetSettlementMoney(account.Id, pdate);
                    change.DayCount = _money.CloseProfit - _money.Commission;
                    change.Free = _money.Commission;
                    change.LoseCount = tth.GetDayLoseCount(pdate, account.Id);
                    change.Profit = _money.CloseProfit;
                    change.SumLost = tth.GetDayAllLose(pdate, account.Id);
                    change.SumWin = tth.GetDayAllWin(pdate, account.Id);
                    change.TradeHistoryId = tradeHistoryId;
                    change.UsedMargin = _money.Margin;
                    change.WinCount = tth.GetDayWinCount(pdate, account.Id);
                    db.AccountDayChange.Add(change);
                }
            }
            db.SaveChanges();
        }

        public Models.Day.AccountDayChange GetAccountDayChange(int tradeHistoryId, int accountid)
        {
            Models.Day.AccountDayChange returnValue = new Models.Day.AccountDayChange();
            var query = db.AccountDayChange.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                returnValue = query.First();
            }
            return returnValue;
        }
    }
}